package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.ProductDetailMapper;
import com.atguigu.spzx.manager.mapper.ProductMapper;
import com.atguigu.spzx.manager.mapper.ProductSkuMapper;
import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;



    @Override
    public PageInfo<Product> getProductPageList(Integer page, Integer limit, ProductDto productDto) {
        PageHelper.startPage(page,limit);
        List<Product> productList = productMapper.findProductPageList(productDto);
        return new PageInfo<>(productList);
    }

    @Override
    public void saveProduct(Product product) {
        //product里面有三个表的数据  需要分开处理,首先需要插入product表,拿到productId.后面两张表需要productId
        // 保存商品数据
        product.setStatus(0);              // 设置上架状态为0
        product.setAuditStatus(0);         // 设置审核状态为0
        product.setAuditMessage("商品待上架");
        //1.插入product表,拿到回显id
        productMapper.insertProduct(product);
        //2.插入product_detail 表
        Long productId = product.getId();
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(productId);
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailMapper.insertProductDetail(productDetails);
        //3.插入product_sku表   .注意这里的传入的sku货物是个集合,有些字段要单独设置
        //3.1 sku_code 需要productId_index这个格式
        //3.2 sku_name 需要拼接
        List<ProductSku> productSkuList=product.getProductSkuList();
        int index=0;
        for (ProductSku productSku : productSkuList) {
            productSku.setProductId(productId);
            productSku.setSkuCode(productId+"_"+index);
            productSku.setSkuName(product.getName()+productSku.getSkuSpec());
            productSku.setSaleNum(0);                               // 设置销量
            productSku.setStatus(0);
            productSkuMapper.insertSku(productSku);
        }

    }

    @Override
    public Product getById(Long productId) {
        Product product= productMapper.selectById(productId);
        //查找到相关sku数据并封装进去
        List<ProductSku> productSkuList = productSkuMapper.selectByProductId(productId);
        product.setProductSkuList(productSkuList);
        //查找到相关details数据并封装进去
        ProductDetails productDetails = productDetailMapper.selectByProductId(productId);
        product.setDetailsImageUrls(productDetails.getImageUrls());
        return product;
    }

    @Override
    public void updateAuditStatus(Long id, Integer status) {
        String auditMessage= (status==1)?"审核通过":"审核不通过";
        productMapper.updateAuditStatus(id,status,auditMessage);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        productMapper.updateStatus(id,status);
    }

    @Override
    public void deleteById(Long id) {
        //商品有三张表,分别是商品表,sku表,details表
        productMapper.deleteById(id);
        productDetailMapper.deleteByProductId(id);
        productSkuMapper.deleteByProductId(id);
    }

    /**
     * 修改产品信息需要覆盖三张表.product,sku,details
     * @param product
     */
    @Override
    public void updateById(Product product) {
        //更改产品表
        productMapper.updateById(product);
        //更改sku表
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (ProductSku productSku : productSkuList) {
            productSkuMapper.updateById(productSku);
        }
        //更改detail表
        ProductDetails productDetails = new ProductDetails();
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetails.setProductId(product.getId());
        productDetailMapper.updateById(productDetails);
    }
}
